package cn.cdeden.aiqa.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.cdeden.common.idempotent.annotation.RepeatSubmit;
import cn.cdeden.common.log.annotation.Log;
import cn.cdeden.common.web.core.BaseController;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.validate.AddGroup;
import cn.cdeden.common.core.validate.EditGroup;
import cn.cdeden.common.log.enums.BusinessType;
import cn.cdeden.common.excel.utils.ExcelUtil;
import cn.cdeden.aiqa.domain.vo.SysKnowledgeConfigVo;
import cn.cdeden.aiqa.domain.bo.SysKnowledgeConfigBo;
import cn.cdeden.aiqa.service.ISysKnowledgeConfigService;
import cn.cdeden.common.core.utils.page.TableDataInfo;

/**
 * 知识库配置
 * 前端访问路由地址为:/aiqa/knowledgeConfig
 *
 * @author Nick
 * @date 2024-12-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledgeConfig")
public class SysKnowledgeConfigController extends BaseController {

    private final ISysKnowledgeConfigService sysKnowledgeConfigService;

    /**
     * 查询知识库配置列表
     */
    @SaCheckPermission("know:knowledgeConfig:list")
    @GetMapping("/list")
    public TableDataInfo<SysKnowledgeConfigVo> list(SysKnowledgeConfigBo bo, PageQuery pageQuery) {
        return sysKnowledgeConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库配置列表
     */
    @SaCheckPermission("know:knowledgeConfig:export")
    @Log(title = "知识库配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysKnowledgeConfigBo bo, HttpServletResponse response) {
        List<SysKnowledgeConfigVo> list = sysKnowledgeConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库配置11", SysKnowledgeConfigVo.class, response);
    }

    /**
     * 获取知识库配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("know:knowledgeConfig:query")
    @GetMapping("/{id}")
    public R<SysKnowledgeConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysKnowledgeConfigService.queryById(id));
    }

    /**
     * 根据参数键名查询参数值
     *
     * @param configKey 参数Key
     */
    @GetMapping(value = "/configKey/{configKey}")
    public R<String> getConfigKey(@PathVariable String configKey) {
        return R.ok("操作成功", sysKnowledgeConfigService.selectConfigByKey(configKey));
    }

    /**
     * 新增知识库配置
     */
    @SaCheckPermission("know:knowledgeConfig:add")
    @Log(title = "知识库配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysKnowledgeConfigBo bo) {
        return toAjax(sysKnowledgeConfigService.insertByBo(bo));
    }

    /**
     * 修改知识库配置
     */
    @SaCheckPermission("know:knowledgeConfig:edit")
    @Log(title = "知识库配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysKnowledgeConfigBo bo) {
        return toAjax(sysKnowledgeConfigService.updateByBo(bo));
    }

    /**
     * 根据参数键名修改参数配置
     */
    @SaCheckPermission("know:knowledgeConfig:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateByKey")
    public R<Void> updateByKey(@RequestBody SysKnowledgeConfigBo config) {
        sysKnowledgeConfigService.updateConfig(config);
        return R.ok();
    }

    /**
     * 删除知识库配置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("know:knowledgeConfig:remove")
    @Log(title = "知识库配置11", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysKnowledgeConfigService.deleteWithValidByIds(List.of(ids), true));
    }
}
